package com.sjb.cp.manager.sharding;

import cn.hutool.core.util.StrUtil;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

/**
 * @Author:sjb
 * @CreateTime:2025-02-19
 * @Description: 动态分库分表算法
 * @Version：1.0
 */
public class PictureShardingAlgorithm implements StandardShardingAlgorithm<Long> {


    @Override
    public String doSharding(Collection<String> availableTableNames, PreciseShardingValue<Long> preciseShardingValue) {
        Long spaceId = preciseShardingValue.getValue();
        String logicTableName = preciseShardingValue.getLogicTableName();
        if (StrUtil.isEmpty(logicTableName)){
            return logicTableName;
        }
        // 生成分表的表名
        String realTableName = "picture_" + spaceId;
        if (availableTableNames.contains(realTableName)){
            return realTableName;
        }else {
            return logicTableName;
        }
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {
        return Collections.emptyList();
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public void init(Properties properties) {

    }
}
